package com.tm.exception.utils;



import com.auth0.jwt.JWTSigner;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.internal.com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class JwtUtils {
    /* 设置秘钥    为了简单 所有都公用一个秘钥  不用的用户 设置不同的秘钥  为了安全   */
    private static final String SECRET = "XX#$%()(#*!()!KL<><MQLMNQNQJQK sdfkjsdrow32234545fdf>?N<:{LWPW";

    private static final String EXP = "exp";

    private static final String PAYLOAD = "payload";

    /**
     * get jwt String of object
     * @param object
     *            the POJO object
     * @param maxAge
     *            the seconds of life time
     * @return the jwt token
     */
    /*  给一个信息   设置时效性  （） 得到对应的token   加密后的密文  */
    public static <T> String createToken(T object, long maxAge) {
        try {
            final JWTSigner signer = new JWTSigner(SECRET);
            final Map<String, Object> claims = new HashMap<String, Object>();
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = mapper.writeValueAsString(object);
            claims.put(PAYLOAD, jsonString);
            /*  默认的是毫秒   改成 乘 1000  */
            claims.put(EXP, System.currentTimeMillis() + maxAge*1000);
            return signer.sign(claims);
        } catch(Exception e) {
            return null;
        }
    }


    /**
     * get the object of jwt if not expired
     * @param jwt
     * @return POJO object
     */
    //解密token
    public static<T> T unsign(String jwt, Class<T> classT) {
        final JWTVerifier verifier = new JWTVerifier(SECRET);
        try {
            final Map<String,Object> claims= verifier.verify(jwt);
            if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)) {
                long exp = (Long)claims.get(EXP);
                long currentTimeMillis = System.currentTimeMillis();
                if (exp > currentTimeMillis) {
                    String json = (String)claims.get(PAYLOAD);
                    ObjectMapper objectMapper = new ObjectMapper();
                    return objectMapper.readValue(json, classT);
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        // 验证一下  加密的信息  任何对象  javabean  或者  map
      /*  HashMap user=new HashMap();
        user.put("name","张三");
        user.put("age",12);
        user.put("sex",'男');
        //告诉加密信息  设置时效性 秒
        // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzI5OTM5MDEwMTcsInBheWxvYWQiOiJ7XCJzZXhcIjpcIueUt1wiLFwibmFtZVwiOlwi5byg5LiJXCIsXCJhZ2VcIjoxMn0ifQ.X8oRgsitITMStPwv_CmoqHsRC6K2wO_9yUuXJoZqD2Y
        // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzI5OTQyMTMzMzYsInBheWxvYWQiOiJ7XCJzZXhcIjpcIueUt1wiLFwibmFtZVwiOlwi5byg5LiJXCIsXCJhZ2VcIjoxMn0ifQ.VwGGE36O84S3eC-7JnLW5YtWSqFSd5peH6b_YEllis8
        String token = JwtUtils.createToken(user, 150);
        System.out.println(token);*/

        //得到密文  解密
      /*  String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzI5OTQyMTMzMzYsInBheWxvYWQiOiJ7XCJzZXhcIjpcIueUt1wiLFwibmFtZVwiOlwi5byg5LiJXCIsXCJhZ2VcIjoxMn0ifQ.VwGGE36O84S3eC-7JnLW5YtWSqFSd5peH6b_YEllis8";
        //当解密后  得到的是null  说明啥问题 token是无效的
        Map unsign = JwtUtils.unsign(token, Map.class);
        System.out.println(unsign);*/

      /*  String info="hello world";
        String token = JwtUtils.createToken(info, 1500);
        System.out.println(token);*/

        //解密
        //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzI5OTYyMjEyNTIsInBheWxvYWQiOiJcImhlbGxvIHdvcmxkXCIifQ.oR5yJyYnaYiL_ndWNBULXaiSsADrXVzC-doDgdY7Dqs
       // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzI5OTYyNDA1OTEsInBheWxvYWQiOiJcImhlbGxvIHdvcmxkXCIifQ.2kQlF11myEZiaBEio-8t3ekC3bcfM_lB5yYwTepr_Zc
        //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzI5OTYyNTk0NDYsInBheWxvYWQiOiJcImhlbGxvIHdvcmxkXCIifQ.OSRGw8YyNS6NZ49MfnyODKiDDEOSRKEwrtPJS5ohRlw
          String unsign = JwtUtils.unsign("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzI5OTYyNDA1OTEsInBheWxvYWQiOiJcImhlbGxvIHdvcmxkXCIifQ.2kQlF11myEZiaBEio-8t3ekC3bcfM_lB5yYwTepr_Zc", String.class);
        System.out.println(unsign);

    }

}
